import datetime


def weekdaystrftime(day):
    if day == 1:
        return "周一"
    elif day == 2:
        return "周二"
    elif day == 3:
        return "周三"
    elif day == 4:
        return "周四"
    elif day == 5:
        return "周五"
    elif day == 6:
        return "周六"
    elif day == 7:
        return "周日"


def get_fmt_data(days) -> dict:
    keyday = {}
    # // 获取当前时间
    today = datetime.datetime.now()
    for i in range(0, days):
        # // 计算偏移量
        offset = datetime.timedelta(days=+i)
        # // 获取想要的日期的时间
        weekday = (today + offset).weekday() + 1
        re_date = (today + offset).strftime('%Y-%m-%d')
        fm_date = (today + offset).strftime('%m.%d')
        keyday[re_date] = {
            'weekday': weekdaystrftime(weekday),
            'date': re_date,
            'fm_date': fm_date,
        }
    return keyday


def get_fmt_data_for_ampm(days) -> dict:
    keyday = {}
    # // 获取当前时间
    today = datetime.datetime.now()
    for i in range(0, days):
        # // 计算偏移量
        offset = datetime.timedelta(days=+i)
        # // 获取想要的日期的时间
        weekday = (today + offset).weekday() + 1
        re_date = (today + offset).strftime('%Y-%m-%d')
        fm_date = (today + offset).strftime('%m.%d')
        for ampm in ['am', 'pm']:
            keyday[f'{ampm}{re_date}'] = {
                'ampmtype': ampm,
                'weekday': weekdaystrftime(weekday),
                'date': re_date,
                'fm_date': fm_date,
            }
    return keyday


def get_fmt_data_for_ampm_in(days, ampm) -> list:
    keyday = []
    # // 获取当前时间
    today = datetime.datetime.now()
    for i in range(0, days):
        # // 计算偏移量
        offset = datetime.timedelta(days=+i)
        # // 获取想要的日期的时间
        weekday = (today + offset).weekday() + 1
        re_date = (today + offset).strftime('%Y-%m-%d')
        fm_date = (today + offset).strftime('%m.%d')
        keyday.append({
            'ampmtypefmt': f'{re_date}{ampm}',
            'ampmtype': ampm,
            'weekday': weekdaystrftime(weekday),
            'date': re_date,
            'fm_date': fm_date,
        })
    return keyday


def get_fmt_data_for_ampm_in_mach_workcycles(appid, days, ampm, sino, dno, workcycles, scheduling_dict,
                                             ispanduancurr=False, ispanduancurrtipmsg='无号') -> list:
    keyday = []
    # // 获取当前时间
    today = datetime.datetime.now()
    for i in range(0, days):
        # // 计算偏移量
        offset = datetime.timedelta(days=+i)
        # // 获取想要的日期的时间
        weekday = (today + offset).weekday() + 1
        re_date = (today + offset).strftime('%Y-%m-%d')
        fm_date = (today + offset).strftime('%m.%d')
        weekday = weekdaystrftime(weekday)
        ampmtypefmt = f'{appid}{sino}{dno}{re_date}{ampm}'
        scheduling_stock = scheduling_dict.get(ampmtypefmt, 0)

        # scheduling_status =  (False if scheduling_stock==-1 or scheduling_stock==-2  else (False if scheduling_stock==0 else True)) if weekday in workcycles else False
        # scheduling_tip = ('过号' if scheduling_stock==-1 else ('无号' if scheduling_stock==-2 else (
        #    "约满" if  scheduling_stock==0 else "可约"
        # ))) if weekday in workcycles else '无号'

        # scheduling_status = IntegerField(constraints=[SQL("DEFAULT 0")], null=True, verbose_name="是否可预约 0 否 1 是")
        scheduling_status = (False if scheduling_stock == 0 else True) if weekday in workcycles else False
        scheduling_tip = ("无号" if scheduling_stock == 0 else "可约") if weekday in workcycles else "无号"

        keyday.append({
            # 标签匹配
            'ampmtypefmt': ampmtypefmt,
            'scheduling_stock': scheduling_stock,
            # 上午和下午的类型
            'ampmtype': ampm,
            # 周几
            'weekday': weekday,
            # 日期
            'date': re_date,
            # 是否可以预约
            'scheduling_status': False if ispanduancurr else scheduling_status,
            # 是否可预约的提示
            'scheduling_tip': ispanduancurrtipmsg if ispanduancurr else scheduling_tip,
            # 对应的时间
            'fm_date': fm_date,
        })
    return keyday


def generate_time_array(start_time, end_time):
    time_array = []
    current_time = start_time
    while current_time <= end_time:
        time_array.append(current_time.strftime('%H:%M'))
        current_time += datetime.timedelta(minutes=30)
    return time_array


def generate_time_array_for_times(start_time, end_time, minutes=30):
    time_array = []
    current_time = start_time
    while current_time <= end_time:
        time_array.append(current_time.strftime('%H:%M'))
        current_time += datetime.timedelta(minutes=minutes)
    return time_array


if __name__ == '__main__':
    # print(get_fmt_data(7))

    for k, v in get_fmt_data_for_ampm(7).items():
        print(k, v)

    start_time = datetime.datetime.strptime('2022-01-01 09:00', '%Y-%m-%d %H:%M')
    end_time = datetime.datetime.strptime('2022-01-01 12:00', '%Y-%m-%d %H:%M')
    time_array = generate_time_array(start_time, end_time)
    print(time_array)
